package com.htj.workbench.web.controller;

import com.htj.setting.domain.User;
import com.htj.setting.service.UserService;
import com.htj.utils.DateTimeUtil;
import com.htj.utils.PrintJson;
import com.htj.utils.UUIDUtil;
import com.htj.workbench.domain.Activity;
import com.htj.workbench.domain.ActivityRemark;
import com.htj.workbench.service.ActivityServiceInf;
import com.htj.workbench.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "Activity")
public class ActivityController {
    @Autowired
    UserService userService;

    @Autowired
    ActivityServiceInf activityService;

    @GetMapping("/getUserList.do")
    public String getUserList(){
        List<User> ulist = userService.getUserList();
        String list = PrintJson.printJsonObj(ulist);
        return list;
    }

    @PostMapping("/Save.do")
    public String Save(Activity activity, HttpServletRequest request){
        String time = DateTimeUtil.getSysTime();
        String name = ((User) request.getSession().getAttribute("user")).getName();
        String uuid = UUIDUtil.getUUID();
        activity.setId(uuid);
        activity.setCreateTime(time);
        activity.setCreateBy(name);
        boolean bool = activityService.Save(activity);
        String json = PrintJson.printJsonFlag(bool);
        return json;
    }
    @GetMapping("/pageList.do")
    public String pageList(HttpServletRequest request){
        String pageNoStr = request.getParameter("pageNo");
        String pageSizeStr = request.getParameter("pageSize");
        String name = request.getParameter("name");
        String owner = request.getParameter("owner");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        /*
            mysql中的limit a,b
            a表示略过多少条,b表示一页显示几条
         */
        int pageNo = Integer.valueOf(pageNoStr);
        int pageSize = Integer.valueOf(pageSizeStr);
        int skipCount = (pageNo - 1) * pageSize;
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("pageSize",pageSize);
        map.put("skipCount",skipCount);
        /*
            前端需要total和ActivityList
            所以服务层需要提供这样的json
            服务层的方法返回值可以是map
            map.put("total",total);
            map.put("ActivityList",ActivityList);
            但是分页获取许多个页面都需要所以可以采用创建一个vo
         */
        PageListVo<Activity> list = activityService.pageList(map);
        String json = PrintJson.printJsonObj(list);
        return json;
    }
    @PostMapping("/delete.do")
    public String delete(HttpServletRequest request){
        String[] ids = request.getParameterValues("id");
        boolean bool = activityService.delete(ids);
        String json = PrintJson.printJsonFlag(bool);
        return json;
    }
    @GetMapping ("/getUserListAndActivity.do")
    public String getUserListAndActivity(HttpServletRequest request){
        String id = request.getParameter("id");
        //前端需要获得uList和a
        //因为这些数据不是经常用所以用一个map封装即可
        Map<String,Object> map = activityService.getUserListAndActivity(id);
        String json = PrintJson.printJsonObj(map);
        return json;
    }
    @PostMapping("/update.do")
    public String update(Activity activity, HttpServletRequest request){
        System.out.println("开始进行更新操作");
        String time = DateTimeUtil.getSysTime();
        String name = ((User) request.getSession().getAttribute("user")).getName();
        activity.setEditTime(time);
        activity.setEditBy(name);
        boolean bool = activityService.update(activity);
        String json = PrintJson.printJsonFlag(bool);
        return json;
    }
    @GetMapping("/detail.do")
    public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("查看活动详情");
        String id =request.getParameter("id");
        Activity a = activityService.detail(id);
        request.setAttribute("a",a);
        request.getRequestDispatcher("/workbench/activity/detail.jsp").forward(request,response);
    }
    @GetMapping("/getRemarkList.do")
    public String getRemarkList(HttpServletRequest request){
        String aid = request.getParameter("ActivityId");
        List<ActivityRemark> list = activityService.getRemarkList(aid);
        String json = PrintJson.printJsonObj(list);
        return json;
    }
    @PostMapping("/deleteRemark.do")
    public String deleteRemark(HttpServletRequest request){
        String id = request.getParameter("id");
        boolean bool = activityService.deleteRemark(id);
        String json = PrintJson.printJsonFlag(bool);
        return json;
    }
    @PostMapping("/saveRemark.do")
    public String saveRemark(ActivityRemark ar,HttpServletRequest request){
        String uuid = UUIDUtil.getUUID();
        String time = DateTimeUtil.getSysTime();
        String name = ((User) request.getSession().getAttribute("user")).getName();
        String editFlag = "0";

        ar.setId(uuid);
        ar.setCreateBy(name);
        ar.setCreateTime(time);
        ar.setEditFlag(editFlag);

        boolean bool = activityService.saveRemark(ar);
        Map<String,Object> map = new HashMap<>();

        map.put("ar",ar);
        map.put("success",bool);
        String json = PrintJson.printJsonObj(map);
        return json;
    }
    @PostMapping("/editRemark.do")
    public String editRemark(ActivityRemark ar,HttpServletRequest request){
        String time = DateTimeUtil.getSysTime();
        String name = ((User) request.getSession().getAttribute("user")).getName();
        String editFlag = "1";

        ar.setEditFlag(editFlag);
        ar.setEditBy(name);
        ar.setEditTime(time);

        boolean bool = activityService.editRemark(ar);
        Map<String,Object> map = new HashMap<>();

        map.put("ar",ar);
        map.put("success",bool);
        String json = PrintJson.printJsonObj(map);
        return json;
    }
}
